<?php

namespace app\admin\validate;

use app\common\business\Category;
use app\common\lib\Captcha;
use think\Validate;

class Notice extends Validate
{
    protected $rule = [
        'id' => 'require',
        'title' => 'require',
        //'face_url' => 'require',
        'category_id' => 'require|checkCategory',
        //'description' => 'require',
        'content' => 'require',
    ];

    protected $scene=[
        'create'=>['title','category_id','content'],
        'update'=>['id','title','category_id','content'],
    ];

    protected $message = [
        'id' => 'ID不能为空',
        'title' => '名称不能为空',
        //'face_url' => '封面图不能为空',
        'category_id' => '分类不能为空',
        //'description' => '描述不能为空',
        'content' => '内容不能为空',
    ];

    protected function checkCategory($value, $rule, $data = [])
    {
        return $this->checkCommonCategory('新闻分类',$value);
    }
    protected function checkCommonCategory($name,$value)
    {
        try {
            (new Category())->checkValidValueByName($name,$value);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return true;
    }
}
